package com.qsmx.qingqiuticket.app.delegate.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qsmx.qingqiuticket.app.R;
import com.qsmx.qingqiuticket.app.delegate.holder.UsbPrintTestHolder;

import java.util.List;

public class UsbPrintTestAdapter extends RecyclerView.Adapter<UsbPrintTestHolder> {

    private Context mContext;

    private List<String> mData;

    //定义接口
    public static interface OnItemClickListener {
        void onClick(View view, int position);

    }

    //声明自定义的监听接口
    private UsbPrintTestAdapter.OnItemClickListener monItemClickListener = null;

    //提供set方法
    public void setonItemClickListener(UsbPrintTestAdapter.OnItemClickListener listener) {
        this.monItemClickListener = listener;
    }

    public UsbPrintTestAdapter(Context context) {
        this.mContext = context;
    }

    public void setDate(List<String> data) {
        this.mData = data;
    }

    @NonNull
    @Override
    public UsbPrintTestHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new UsbPrintTestHolder(LayoutInflater.from(mContext).inflate(R.layout.usb_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull UsbPrintTestHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.usbName.setText(mData.get(position));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (monItemClickListener != null) {
                    monItemClickListener.onClick(view, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mData != null && !mData.isEmpty()) {
            return mData.size();
        } else {
            return 0;
        }
    }
}
